'''
Case Type： 故障&可靠性测试
Case Name： 数据库备机异常情况下（控制文件权限丢失），进程备升主；
Create At: 2020/07/20
@zou_jialiang0505328114
Descption:
步骤 1.启动主备数据库，判断数据库状态；
步骤 2.模拟备机数据库异常情况（控制文件权限丢失）。
步骤 3.进行备升主(可能出现两种结果，checkpoint有固定刷新时间，如果刚好checkpoint时控制权限没有会直接导致备机stop)
步骤 4.如果备机已经stop，恢复权限，重启备机（switchover未进行）
步骤 4.如果备机未stop，switchover预期超时，重启主备；（主已经降备，备升不了主）
步骤 5.恢复权限及环境后，再次进行切换成功
步骤 6.恢复主备关系
'''

import unittest
from yat.test import macro
from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import *


class Deletaduit(unittest.TestCase):
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')
    primary_sh = CommonSH('PrimaryDbUser')
    standby_1_sh = CommonSH('Standby1DbUser')
    @RestartDbCluster(*nodes_tuple)

    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case150.py start------------')
        self.Constant = Constant()
        self.primary_root = Node(node='PrimaryRoot')
        self.primary_dbuser = Node(node='PrimaryDbUser')
        self.standby1_root = Node(node='Standby1Root')
        self.standby1_dbuser = Node(node='Standby1DbUser')

    def test_startdb(self):

        self.log.info("-----------模拟备机数据库异常情况（控制文件权限丢失）-----------")
        self.log.info('chmod 000 ' + macro.DB_INSTANCE_PATH + '/global/pg_control*')
        self.log.info(self.standby1_root.sh('chmod 000 ' + macro.DB_INSTANCE_PATH + '/global/pg_control*').result())
        self.log.info(self.standby1_root.sh('ls -l '+macro.DB_INSTANCE_PATH + '/global/pg_control*').result())

        self.log.info("-----------查询集群状态-----------")
        status = self.primary_sh.getDbClusterStatus('detail')
        self.log.info(status)

        self.info = self.log.info("-----------进行备升主-----------")
        result = self.standby_1_sh.executeGsctl('switchover','','',True)
        if self.Constant.SWITCHOVER_CANNOT_MSG in result:
            self.log.info('---------备机stop，无法切换-------')
            self.log.info('---------进行权限恢复-------')
            self.log.info('chmod 600 ' + macro.DB_INSTANCE_PATH + '/global/pg_control*')
            self.log.info(self.standby1_root.sh('chmod 600 ' + macro.DB_INSTANCE_PATH + '/global/pg_control*').result())
            self.log.info('---------启动备机-------')
            is_started = self.standby1_dbuser.startDbCluster()
            self.assertTrue(is_started)

        elif self.Constant.SWITCHOVER_FAIL_MSG in result:
            self.log.info('---------备机未stop，无法切换-------')
            self.log.info('---------进行权限恢复-------')
            self.log.info('chmod 600 ' + macro.DB_INSTANCE_PATH + '/global/pg_control*')
            self.log.info(self.standby1_root.sh('chmod 600 ' + macro.DB_INSTANCE_PATH + '/global/pg_control*').result())
            self.log.info('---------重启主备-------')
            stopMsg = self.primary_sh.stopDbCluster()
            self.assertTrue(stopMsg)
            is_started = self.primary_sh.startDbCluster()
            self.assertTrue(is_started)

        else:
            self.log.info('---------其他不符合预期异常场景，需要手工定位-------')
            self.log.info('---------进行权限恢复-------')
            self.log.info('chmod 600 ' + macro.DB_INSTANCE_PATH + '/global/pg_control*')
            self.log.info(self.standby1_root.sh('chmod 600 ' + macro.DB_INSTANCE_PATH + '/global/pg_control*').result())
            self.assertTrue(False)

        self.log.info('---------恢复权限及环境后再次进行切换成功-------')
        is_switched = self.standby_1_sh.executeGsctl('switchover',self.Constant.SWITCH_SUCCESS_MSG)
        self.assertTrue(is_switched)

    def tearDown(self):
        self.log.info('----------------this is tearDown-----------------------')
        self.log.info("-----------进行权限恢复-----------")
        self.standby1_root.sh('chmod 600 ' + macro.DB_INSTANCE_PATH + '/global/pg_control*')
        self.log.info(self.standby1_root.sh('ls -l '+macro.DB_INSTANCE_PATH + '/global/pg_control*').result())
        self.log.info('---------恢复主备关系-------')
        host_name = self.primary_root.sh('hostname').result()
        if self.primary_sh.check_whether_need_switch(host_name):
            is_switched = self.primary_sh.executeGsctl('switchover', self.Constant.SWITCH_SUCCESS_MSG)
            self.assertTrue(is_switched)
        self.log.info('------------------------------------reliability_dbsys150.py finish------------------------------------')
